function google_map(latlng) {
    var latlng = latlng.split(",");
    var lat = latlng[0];
    var lng = latlng[1];

    var center_position = new google.maps.LatLng(lat, lng);
    var marker_position = new google.maps.LatLng(lat, lng);
    var marker;
    var map;

    var mapOptions = {
        zoom: 15,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        center: center_position,

        panControl: false,
        mapTypeControl: false,
        zoomControl: true,
        zoomControlOptions: {
            style: google.maps.ZoomControlStyle.SMALL,
            position: google.maps.ControlPosition.TOP_RIGHT
        },
        streetViewControl: false,
        scaleControl: false,
        rotateControl: false,
        overviewMapControl: false
    };

    map = new google.maps.Map(document.getElementById('map'), mapOptions);

    marker = new google.maps.Marker({
        map:map,
        draggable:false,
        animation: google.maps.Animation.DROP,
        position: marker_position,
        icon : new google.maps.MarkerImage('/files/img/red_flag.png',
            new google.maps.Size(69,48),
            new google.maps.Point(0,0),
            new google.maps.Point(35,48)
        ),
        shadow: new google.maps.MarkerImage('/files/img/red_flag_shadow.png',
            new google.maps.Size(97,48),
            new google.maps.Point(0,0),
            new google.maps.Point(35,48)
        )
    }); 



    var boxText = document.createElement("div");
    boxText.style.cssText = "border: 2px solid #357EBD; margin-top: 8px; background: #fff; padding: 5px; font-size: 12px";
    boxText.innerHTML = infoBoxHtml;

    var infoBoxOptions = {
        content: boxText
        ,disableAutoPan: false
        ,maxWidth: 0
        ,pixelOffset: new google.maps.Size(-140, 0)
        ,zIndex: null
        ,boxStyle: { 
            background: 'url("/files/img/map/tipbox.gif") no-repeat scroll 133px 0 transparent'
            ,opacity: 1
            ,width: "280px"
        }
        ,closeBoxMargin: "12px 4px 4px 4px"
        ,closeBoxURL: "/files/img/map/close.gif"
        ,infoBoxClearance: new google.maps.Size(1, 1)
        ,isHidden: false
        ,pane: "floatPane"
        ,enableEventPropagation: false
    };
    var infoBox = new InfoBox(infoBoxOptions);
    infoBox.open(map, marker);

    google.maps.event.addListener(marker, "click", function (e) {
        infoBox.open(map, this);
    });

    google.maps.event.trigger(map, 'resize');
    map.setZoom(map.getZoom());
    //        map.fitBounds(bounds);
}

function mCustomScrollbarInit(){
    $(".image-gallery").mCustomScrollbar({
        scrollInertia:300,
        horizontalScroll:true,
        advanced:{

            //updateOnBrowserResize:true, /*update scrollbars on browser resize (for layouts based on percentages): boolean*/
            updateOnContentResize:false, /*auto-update scrollbars on content resize (for dynamic content): boolean*/
            autoExpandHorizontalScroll:true, /*auto-expand width for horizontal scrolling: boolean*/
            //autoScrollOnFocus:true, /*auto-scroll on focused elements: boolean*/
            //normalizeMouseWheelDelta:false /*normalize mouse-wheel delta (-1/1)*/
        },
        scrollButtons:{
            enable:true
        },
        theme:"dark-thick"
    });
}
$(function(){
    mCustomScrollbarInit();
    
//    $('.text-content img').each(function(index){
//        if($(this).attr('src').substr(0, 7) == '/upload'){
//            $(this).wrap('<div class="image-frame"></div>');    
//        }    
//    });
});


$('#post-reply-form').submit(function(){
    $('#PostReply_content').val(tinyMCE.activeEditor.getContent());

});
function postReplyAfterValidate(form, data, hasError){
    if(hasError){
        var focusSelector;
        $.each(data, function(key, value){
            focusSelector = '#'+key;
            $.each(value, function(i, msg){
                myGrowl(msg, 'error');
            });        
        });   
        $(focusSelector).focus();
        scrollToAnchor(focusSelector);   
        return false;
        
    }else{
        $('#ajax-loading').fadeIn();
        $.post($("#post-reply-form").attr("action"), $("#post-reply-form").serialize(), function(data){
            if(data.status == "success"){
                $("#PostReply_content").val("");
                tinyMCE.activeEditor.setContent('');
                
                if($(data.afterSelector).length){
                    $(data.afterSelector).after(data.replyView);
                }else{
                    $('#panel-replies').removeClass('hide').addClass('show').fadeIn();
                    $('#panel-body-replies').after(data.replyView);
                }   
                mCustomScrollbarInit();
                scrollToAnchor("#reply_"+data.postReply.id);
                myGrowl(data.message, data.status); 
                $('#reply_to_user').parent().fadeOut('slow', function(){
                    $('#reply_to_user').text('');
                    $('#PostReply_reply_id').val('');
                }); 
            }
            $('#ajax-loading').fadeOut();    
        }, 'json');
        
        //return true;
    }
}
$('#btn-reply').click(function(){
    scrollToAnchor('#panel-reply');
    tinyMCE.execCommand('mceFocus', false, 'PostReply_content');
});

$('.post-reply').live('click', function(){
    scrollToAnchor('#panel-reply');
    tinyMCE.execCommand('mceFocus', false, 'PostReply_content');
    $('#reply_to_user').text('@'+$(this).attr('user_name'));
    $('#PostReply_reply_id').val($(this).attr('reply_id'));
    $('#reply_to_user').parent().fadeIn('slow');
});
$('#reply_to_user_remove').click(function(){
    tinyMCE.execCommand('mceFocus', false, 'PostReply_content');
    $(this).parent().fadeOut('slow', function(){
        $('#reply_to_user').text('');
        $('#PostReply_reply_id').val('');
    });
});



$('#reply-advance').click(function(e){
    e.preventDefault();
    var reply_id = $('#PostReply_reply_id').val();
    var post_id = $('#PostReply_post_id').val();
    var content = tinyMCE.activeEditor.getContent();
    var replyUrl = $(this).attr('rel');
    if(reply_id != ''){
        replyUrl += '/'+reply_id;    
    }

    if(!$('#form-reply-advance').length){
        $('body').append("<form id='form-reply-advance' action='"+replyUrl+"' method='post'><input type='hidden' name='post_id' value='"+post_id+"'><input type='hidden' name='reply_id' value='"+reply_id+"'><input type='hidden' name='content' value='"+content+"'></form>");
    }
    $('#form-reply-advance').submit();
});

// required bootbox.min.js
// http://bootboxjs.com/documentation.html
$('.confirm-delete-reply').click(function(e) {
    e.preventDefault();
    var replyDeleteUrl = $(this).attr('rel');
    var replyId = $(this).attr('reply_id');
    bootbox.confirm({
        message: "Bạn có chắc chắn muốn xóa nội dung trả lời này ?",
        buttons: {
            confirm: {
                label: "<i class='fa fa-trash-o'></i> Chắc chắn xóa",
                className: "btn-danger",
            },
            cancel: {
                label: "Không xóa",
                className: "btn-default",
            }
        },
        callback: function(result){
            if(result){
                $('#reply_'+replyId).slideUp('slow');
                myGrowl('Bạn đã xóa trả lời thành công', 'success');    
                $.post(replyDeleteUrl);
            }
        }
    });  
});